//
//  AppCell.m
//  LimitFree
//
//  Created by LZXuan on 15-6-25.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "AppCell.h"
#import "UIImageView+WebCache.h"

@implementation AppCell

//xib 初始化加载的时候调用
//创建 xib 视图的时候调用
- (void)awakeFromNib {
    // Initialization code
    //初始化代码
    //设置圆角
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = 8;
    //设置选中背景
    self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"appproduct_loadingviewcell_ligh_2t"]];
    
    //初始化的创建一条线在label 上
    CGRect frame = self.priceLabel.frame;
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height/2, frame.size.width, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [self.priceLabel addSubview:lineView];
}

//每次显示cell 都会调用 重新赋值
- (void)showDataWithModel:(AppModel *)model indexPath:(NSIndexPath *)indexPath {
    self.nameLabel.text = model.name;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl] placeholderImage:[UIImage imageNamed: @"topic_Header"]];
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f",model.lastPrice.doubleValue];
    self.categoryNameLabel.text = model.categoryName;
    //星级
    [self.starLevel setStarLevel:model.starOverall.doubleValue];
    self.sharesLabel.text = [NSString stringWithFormat:@"分享:%@ 收藏:%@ 下载:%@",model.shares,model.favorites,model.downloads];
    if ([self.categoryType isEqualToString:kLimitType]) {
        //限免类型
        self.typeLabel.text = [NSString stringWithFormat:@"剩余:%@",[LZXHelper stringNowToDate:model.expireDatetime formater:@"yyyy-MM-dd HH:mm:ss.0"]];
    }else if ([self.categoryType isEqualToString:kReduceType]) {
        self.typeLabel.text = [NSString stringWithFormat:@"现价:%@",model.currentPrice];
    }else {
        self.typeLabel.text = [NSString stringWithFormat:@"评分:%@",model.starCurrent];
    }
    
    //添加背景
    if (indexPath.row%2) {
        //图片拉伸 方法  常常用于气泡聊天
        //stretchableImageWithLeftCapWidth :80 topCapHeight:75
        //从图片的 距离左侧 80像素 保持不变 第81像素进行 水平拉伸 其他像素不变
        //从图片的 距离顶部 75     保持不变 第76像素进行 竖直拉伸 其他像素不变
        UIImage *image = [[UIImage imageNamed: @"cate_list_bg1"] stretchableImageWithLeftCapWidth:80 topCapHeight:75];
        self.backgroundView = [[UIImageView alloc] initWithImage:image];
    }else  {
        UIImage *image = [[UIImage imageNamed: @"cate_list_bg2"] stretchableImageWithLeftCapWidth:80 topCapHeight:75];
        self.backgroundView = [[UIImageView alloc] initWithImage:image];
    }
    UIView *lineView = self.priceLabel.subviews[0];
    if (model.lastPrice.doubleValue == 0.0) {
        lineView.hidden = YES;
    }else{
        lineView.hidden = NO;
    }
}
//当 cell 被选中/被点击(高亮状态)的时候 系统会自动的把cell 的contentView 的所有子视图的颜色设置为 clearColor

//那么 如果我们 不想 让 cell的子视图颜色改变 那么需要重写 下面的方法


//cell 被选中的时候调用
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    //cell 被选中 时 设置 线条还是黑色
    UIView *lineView = self.priceLabel.subviews[0];
    lineView.backgroundColor = [UIColor blackColor];
}
//点击cell (高亮状态)
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    //cell 被选中 时 设置 线条还是黑色
    UIView *lineView = self.priceLabel.subviews[0];
    lineView.backgroundColor = [UIColor blackColor];
    
}

@end
